package com.sf.fy.uav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.enums.SettingStatusEnum;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.operate.SysNoticeRequest;
import com.sf.fy.uav.common.request.order.DealRequest;
import com.sf.fy.uav.common.response.operational.SysNoticeResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.entity.Notice;
import com.sf.fy.uav.mapper.NoticeMapper;
import com.sf.fy.uav.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2024-08-08
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public List<Notice> getNotice() {
        Date date = new Date();
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.ne("status", SettingStatusEnum.OFF.getStatus());
        noticeQueryWrapper.eq("deleted", false);
        noticeQueryWrapper.orderByAsc("sort");
        noticeQueryWrapper.le("start_time", date);
        noticeQueryWrapper.ge("end_time", date);
        return noticeMapper.selectList(noticeQueryWrapper);
    }

    @Override
    public Long getTotal(String name, Integer status) {
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(name)) {
            noticeQueryWrapper.like("name", name);
        }
        if (!ObjectUtils.isEmpty(status)) {
            noticeQueryWrapper.eq("status", status);
        }
        noticeQueryWrapper.eq("deleted", false);
        return noticeMapper.selectCount(noticeQueryWrapper);
    }

    @Override
    public List<SysNoticeResponse> getNotices(SysNoticeRequest sysNoticeRequest) {
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        Page<Notice> noticePage = new Page<>(sysNoticeRequest.getPage(), sysNoticeRequest.getSize());
        String name = sysNoticeRequest.getName();
        Integer status = sysNoticeRequest.getStatus();
        if (!ObjectUtils.isEmpty(name)) {
            noticeQueryWrapper.like("name", name);
        }
        if (!ObjectUtils.isEmpty(status)) {
            noticeQueryWrapper.eq("status", status);
        }
        noticeQueryWrapper.eq("deleted", false);
        noticeQueryWrapper.orderByDesc("update_time");
        IPage<Notice> noticeIPage = noticeMapper.selectPage(noticePage, noticeQueryWrapper);
        List<Notice> notices = noticeIPage.getRecords();
        List<SysNoticeResponse> sysNoticeResponses = BeanUtil.copyProperties(notices, SysNoticeResponse.class);
        return sysNoticeResponses;
    }

    @Override
    public void off(DealRequest dealRequest) {
        Notice notice = noticeMapper.selectById(dealRequest.getId());
        if (ObjectUtils.isEmpty(notice)) {
            throw new RuntimeException("notice不存在");
        }
        notice.setProcessedBy(dealRequest.getProcessedBy());
        notice.setStatus(SettingStatusEnum.OFF.getStatus());
        notice.setUpdateTime(new Date());
        noticeMapper.updateById(notice);
    }

    @Override
    public Integer start(IdRequest idRequest) {
        Notice notice = noticeMapper.selectById(idRequest.getId());
        if (!ObjectUtils.isEmpty(notice)) {
            Date startTime = notice.getStartTime();
            Date endTime = notice.getEndTime();
            Date date = new Date();
            if (startTime.getTime() > date.getTime()) {
                //还没开始
                notice.setStatus(SettingStatusEnum.NOT_STARTED.getStatus());
            } else {
                if (date.getTime() < endTime.getTime()) {
                    notice.setStatus(SettingStatusEnum.ON_DISPLAY.getStatus());
                } else {
                    notice.setStatus(SettingStatusEnum.ENDED.getStatus());
                }
            }
            noticeMapper.updateById(notice);
            return notice.getStatus();
        }
        return null;
    }

    @Override
    public Integer end(IdRequest idRequest) {
        Notice notice = noticeMapper.selectById(idRequest.getId());
        if (!ObjectUtils.isEmpty(notice)) {
            Date startTime = notice.getStartTime();
            Date endTime = notice.getEndTime();
            Date date = new Date();
            if (startTime.getTime() > date.getTime()) {
                //还没开始
                notice.setStatus(SettingStatusEnum.NOT_STARTED.getStatus());
            } else {
                if (date.getTime() < endTime.getTime()) {
                    notice.setStatus(SettingStatusEnum.ON_DISPLAY.getStatus());
                } else {
                    notice.setStatus(SettingStatusEnum.ENDED.getStatus());
                }
            }
            noticeMapper.updateById(notice);
            return notice.getStatus();
        }
        return null;
    }

    @Override
    public void delete(DealRequest dealRequest) {
        Notice notice = noticeMapper.selectById(dealRequest.getId());
        if (ObjectUtils.isEmpty(notice)) {
            throw new RuntimeException("notice不存在");
        }
        notice.setProcessedBy(dealRequest.getProcessedBy());
        notice.setDeleted(true);
        notice.setUpdateTime(new Date());
        noticeMapper.updateById(notice);
    }

    @Override
    public SysNoticeResponse getById(String noticeId) {
        Notice notice = noticeMapper.selectById(noticeId);
        if (ObjectUtils.isEmpty(notice)) {
            throw new RuntimeException("记录不存在");
        }
        return BeanUtil.copyProperties(notice, SysNoticeResponse.class);
    }

    @Override
    public List<Notice> selectNotStartAndIng(Date startTime, Date endTime, String id) {
        Date date = new Date();
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.ne("status", SettingStatusEnum.OFF.getStatus());
        noticeQueryWrapper.eq("deleted", false);
        noticeQueryWrapper.orderByAsc("sort");
        noticeQueryWrapper.ge("end_time", date);
        noticeQueryWrapper.ge("end_time", startTime);
        noticeQueryWrapper.le("start_time", endTime);
        if (!ObjectUtils.isEmpty(id)) {
            noticeQueryWrapper.ne("id", id);
        }
        return noticeMapper.selectList(noticeQueryWrapper);
    }
}
